package com.service.impl;

import java.util.List;

import com.bean.SysUser;
import com.dao.SysUserDao;
import com.service.SysUserService;
import com.util.EncoderHandler;
import com.util.GUtil;

public class SysUserServiceImpl implements SysUserService {

	@Override
	public SysUser login(String userName, String password, String ip) throws Exception {
		return sysUserDao.login(userName, password, ip);
	}

	@Override
	public List<SysUser> query(SysUser sysUser) throws Exception {
		return sysUserDao.query(sysUser);
	}


	@Override
	public String add(SysUser sysUser) throws Exception {
		sysUser.setUserPasswd( EncoderHandler.EncoderPasswordBase64Md5(sysUser.getUserPasswd()));
		return sysUserDao.save(sysUser);
	}
	@Override
	public String update(SysUser sysUser) throws Exception {
		if(sysUser.getSysUserId()!=null){
			SysUser su = sysUserDao.findById(sysUser.getSysUserId());
			if(su!=null){
				if(sysUser.getUserPasswd()!=null && !sysUser.getUserPasswd().trim().isEmpty())
					sysUser.setUserPasswd( EncoderHandler.EncoderPasswordBase64Md5(sysUser.getUserPasswd()));
				else{
					sysUser.setUserPasswd(su.getUserPasswd());
				}
				return sysUserDao.update(sysUser);
			}
		}
		return GUtil.FAILE;
	}

	@Override
	public String delete(Long id) throws Exception {
		return sysUserDao.delete(id);
	}

	SysUserDao sysUserDao ;

	@Override
	public SysUser get(Long id) throws Exception {
		SysUser user =  this.sysUserDao.findById(id);
		if(user!=null)
			user.setUserPasswd("");
		return user;
	}

	/**
	 * @return the sysUserDao
	 */
	public SysUserDao getSysUserDao() {
		return sysUserDao;
	}

	/**
	 * @param sysUserDao the sysUserDao to set
	 */
	public void setSysUserDao(SysUserDao sysUserDao) {
		this.sysUserDao = sysUserDao;
	}



}
